#!/bin/bash
set -euo pipefail
BaseDir=$(cd "$(dirname "$0")"; pwd)
cd ${BaseDir}

# 使用 go 安装 rootlesskit
# go install github.com/rootless-containers/rootlesskit/cmd/rootlesskit@latest
# 安装 Docker
#curl -fsSL https://get.docker.com -o get-docker.sh
#sh get-docker.sh
###
:<<EOF
rootless技术
https://github.com/rootless-containers/rootlesskit/releases/download/v2.3.1/rootlesskit-x86_64.tar.gz
    rootlesskit-docker-proxy
    rootlesskit
    rootlessctl

docker的解压执行包
https://download.docker.com/linux/static/stable/x86_64/
https://download.docker.com/linux/static/stable/x86_64/docker-27.3.1.tgz
    runc
    dockerd
    docker-proxy
    docker-init
    docker
    ctr
    containerd-shim-runc-v2
    containerd
https://download.docker.com/linux/static/stable/x86_64/docker-rootless-extras-27.3.1.tgz
    vpnkit
    rootlesskit-docker-proxy
    rootlesskit
    dockerd-rootless.sh
    dockerd-rootless-setuptool.sh

debian bullseye的包
https://download.docker.com/linux/debian/dists/bullseye/pool/stable/amd64/
https://download.docker.com/linux/debian/dists/bullseye/pool/stable/amd64/containerd.io_1.7.24-1_amd64.deb /usr/bin
    runc
    ctr
    containerd-shim-runc-v2
    containerd-shim-runc-v1
    containerd-shim
    containerd
https://download.docker.com/linux/debian/dists/bullseye/pool/stable/amd64/docker-ce_27.3.1-1~debian.11~bullseye_amd64.deb /usr/bin
    dockerd
    docker-proxy    
https://download.docker.com/linux/debian/dists/bullseye/pool/stable/amd64/docker-ce-cli_27.3.1-1~debian.11~bullseye_amd64.deb /usr/bin
    docker
https://download.docker.com/linux/debian/dists/bullseye/pool/stable/amd64/docker-ce-rootless-extras_27.3.1-1~debian.11~bullseye_amd64.deb /usr/bin
    rootlesskit-docker-proxy
    rootlesskit
    dockerd-rootless.sh
    dockerd-rootless-setuptool.sh
https://download.docker.com/linux/debian/dists/bullseye/pool/stable/amd64/docker-buildx-plugin_0.17.1-1~debian.11~bullseye_amd64.deb /usr/libexec/docker/cli-plugins/
    docker-buildx    
https://download.docker.com/linux/debian/dists/bullseye/pool/stable/amd64/docker-compose-plugin_2.29.7-1~debian.11~bullseye_amd64.deb /usr/libexec/docker/cli-plugins/
    docker-compose
https://download.docker.com/linux/debian/dists/bullseye/pool/stable/amd64/docker-scan-plugin_0.23.0~debian-bullseye_amd64.deb


https://github.com/rootless-containers/slirp4netns/releases/download/v1.3.1/slirp4netns-x86_64
EOF

#myHome=${1-/cloudide/workspace/devai}
myHome=${1-/home/cloudide}
echo "当前的路径 $myHome 当前用户：$(whoami)"

# 创建 Docker 用户和配置目录
mkdir -p ${myHome}/.docker ${myHome}/.local/share/docker ${myHome}/.docker/cli-plugins.d/

mkdir -p ${myHome}/dockerce
if [ -f ${myHome}/dockerce/docker/dockerd ]; then
    echo "binary files ready"
else
    tar -zxvf ./pkg/docker-27.3.1.tgz -C ${myHome}/dockerce
    tar -zxvf ./pkg/docker-rootless-extras-27.3.1.tgz -C ${myHome}/dockerce

    chmod +x ./pkg/slirp4netns-x86_64
    cp -f ./pkg/slirp4netns-x86_64 ${myHome}/dockerce/docker-rootless-extras/slirp4netns
fi

# 配置 Docker 守护进程
cat <<EOF > ${myHome}/.docker/daemon.json
{
  "data-root": " ${myHome}/.local/share/docker",
  "storage-driver": "vfs"
}
EOF
cat <<EOF > ${myHome}/.docker/cli-plugins.d/docker-compose.conf
[plugin]
path=${myHome}/dockerce/cli-plugins
EOF

export PATH="$PATH:${myHome}/dockerce/docker:${myHome}/dockerce/docker-rootless-extras"
echo "#####"
rootlesskit -v
dockerd -v
docker -v

# 启动 rootlesskit 并运行 Docker
cat <<EOF
rootless运行docker的命令：
# newuidmap
export PATH="/home/cloudide/dockerce/docker:/home/cloudide/dockerce/docker-rootless-extras:$PATH"
➜  devai cat /etc/subuid                                                                
cloudide:100000:65536
➜  devai cat /etc/subgid           
cloudide:100000:65536
export PATH="/home/cloudide/dockerce/docker:/home/cloudide/dockerce/docker-rootless-extras:$PATH"
dockerd-rootless-setuptool.sh install  --skip-iptables
export DOCKER_HOST=unix:///home/cloudide/.docker/run/docker.sock
dockerd-rootless.sh --experimental --storage-driver vfs

export PATH="${myHome}/dockerce/docker:${myHome}/dockerce/docker-rootless-extras:\$PATH"
rootlesskit --net=slirp4netns --copy-up=/etc --copy-up=/run --disable-host-loopback --port-driver=builtin dockerd
EOF
